package com.example.figurehecheng.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.figurehecheng.common.context.BaseContext;
import com.example.figurehecheng.common.result.PageResult;
import com.example.figurehecheng.domain.dto.HousePageDTO;
import com.example.figurehecheng.domain.dto.SaveHouseDTO;
import com.example.figurehecheng.domain.entity.House;
import com.example.figurehecheng.mapper.HouseMapper;
import com.example.figurehecheng.service.IHouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 宇科工作室
 * @since 2024-10-18
 */
@RequiredArgsConstructor
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements IHouseService {

    private final HouseMapper houseMapper;

    /**
     * 查询房间信息
     * @param housePageDTO
     * @return
     */
    @Override
    public PageResult selectHousePage(HousePageDTO housePageDTO) {
        Page<House> page = new Page<>(housePageDTO.getPageNum(), housePageDTO.getPageSize());
        //查询房间信息
        Page<House> housePage = lambdaQuery()
                .like(StrUtil.isNotBlank(housePageDTO.getName()), House::getName, housePageDTO.getName())
                .eq(housePageDTO.getHouseType() != null, House::getHouseType, housePageDTO.getHouseType())
                .eq(House::getHotelId, BaseContext.getCurrentId())
                .page(page);
        List<House> records = housePage.getRecords();
        long total = housePage.getTotal();
        return new PageResult(total, records);
    }

    /**
     * 添加房间信息
     * @param saveHouseDTO
     * @return
     */
    @Override
    public void saveHouse(SaveHouseDTO saveHouseDTO) {
        House house = new House();
        BeanUtils.copyProperties(saveHouseDTO, house);
        house.setHotelId(Math.toIntExact(BaseContext.getCurrentId()));
        save(house);
    }

    /**
     * 修改房间
     * @param saveHouseDTO
     * @return
     */
    @Override
    public void updateHouse(SaveHouseDTO saveHouseDTO) {
        House house = new House();
        BeanUtils.copyProperties(saveHouseDTO, house);
        updateById(house);

    }

    /**
     * 根据id删除房间
     * @param id
     * @return
     */
    @Override
    public void deleteHouse(Integer id) {
        removeById(id);
    }
}
